﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

using System.Xml;
using System.IO;
using System.Net;

using System.Data.SqlClient;
using System.Data;
using System.Security.Cryptography;

namespace PushService
{
    // REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom de classe "Service1" dans le code, le fichier svc et le fichier de configuration.
    public class Service1 : IService1
    {
        // Provide the following information
        private static string dataSource = @"tcp:t72w6m2haz.database.windows.net,1433";
        private static string userName = "TaxiTouch";
        private static string password = "P@ssw0rd";
        SqlConnectionStringBuilder connStringBuilder;

        //Data members.

        //This tracks the devices-to-channelURI list, which gets updated by calls to
        //SubscribeMyPhone();
        private static Dictionary<Guid, Uri> SubscriptionURIs = new Dictionary<Guid, Uri>();

        //Custom constructor
        public Service1()
        {  
            // Create a connection string for the master database
            connStringBuilder = new SqlConnectionStringBuilder();
            connStringBuilder.DataSource = dataSource;
            connStringBuilder.InitialCatalog = "TaxiTouch_db";
            connStringBuilder.Encrypt = true;
            connStringBuilder.TrustServerCertificate = false;
            connStringBuilder.UserID = userName;
            connStringBuilder.Password = password;

            List<Client> allClient = new List<Client>();

            
            //Load from database into SubscriptionURIs here
            allClient = SelectAllClient();

            foreach (Client cli in allClient)
            {
                Uri monURI = new Uri(cli.URIClient.ToString());
                Guid monGUID = new Guid(cli.IDWPClient.ToString());
                //add subscriptions in the dictionnary 
                if (!SubscriptionURIs.ContainsKey(monGUID))
                {
                    SubscriptionURIs.Add(monGUID, monURI);
                }
            }
            
        }

        

        //This overwrites/creates entries for each phone that wants to receive Push Notifications.
        //Note : This list is reset whenever application restarts -- in real life, you will want to 
        //store this data from the SubscriptionURIs list into a database so it persists. Hence,
        //there are TODO sections in this code.

        public Client SubscribeMyPhone(Guid phoneID, string channelURI)
        {
            Client cli = SelectAClient(phoneID);

            if(cli.IDClient.Equals(0))
            {
                Uri thisURI = new Uri(channelURI);
                bool result = false;
                if (SubscriptionURIs.ContainsKey(phoneID))
                {
                    //update the existing URI entry for this phone, if need be ==> validation Windows Markerplace
                    SubscriptionURIs[phoneID] = thisURI;
                    //Update URI in database
                    result = UpdateClientURI(phoneID, thisURI);
                }
                else
                {
                    //otherwise, add a subscription for this phone
                    SubscriptionURIs.Add(phoneID, thisURI);
                    //Save this data in a database, otherwise it will be lost on application restart
                    result = InsertClient(phoneID, thisURI);
                }

                cli = SelectAClient(phoneID);
            }

            return cli;
            
        }

        //Sends the received text value as a Raw notification -- which can only be received 
        //while the subscribed phone app is running. Raw notification have a size limit of 1K.
        public void PushRawData(string rawMessage)
        {
            //Encode the message as as byte[] array as the Notification Service Expects.
            System.Text.UTF8Encoding encoding = new UTF8Encoding();

            //Send this message to all subscribed devices.
            sendPushToSubscribedURIs(encoding.GetBytes(rawMessage), "raw");
        }

        public void PushRawDataUnique(string rawMessage, Uri ClientURI)
        {
            //Encode the message as as byte[] array as the Notification Service Expects.
            System.Text.UTF8Encoding encoding = new UTF8Encoding();

            //Send this message to all subscribed devices.
            sendPushToSpecifiedURI(encoding.GetBytes(rawMessage), "raw", ClientURI);
        }

        //Sends a Toast notification, using the arguments to define the notification's Title and Message.
        public void PushToast(string ToastTitle, string ToastMessage)
        {
            //Use XLMWriter to construct notification structure/contents.
            MemoryStream myStream = new MemoryStream();
            XmlWriter myWriter = XmlWriter.Create(myStream);

            myWriter.WriteStartDocument();

            myWriter.WriteStartElement("wp", "Notification", "WPNotification");
            myWriter.WriteStartElement("wp", "Toast", "WPNotification");
            myWriter.WriteStartElement("wp", "Text1", "WPNotification");
            myWriter.WriteValue(ToastTitle);
            myWriter.WriteEndElement();

            myWriter.WriteStartElement("wp", "Text2", "WPNotification");
            myWriter.WriteValue(ToastMessage);
            myWriter.WriteEndElement();

            myWriter.WriteEndElement();
            myWriter.WriteEndDocument();

            //Transfer Xml Output for myWriter's buffer to myStream.
            myWriter.Flush();

            //Send this message to all subscribed devices.
            sendPushToSubscribedURIs(myStream.ToArray(), "toast");
        }

        public void PushToastUnique(string ToastTitle, string ToastMessage, Uri ClientURI)
        {
            //Use XLMWriter to construct notification structure/contents.
            MemoryStream myStream = new MemoryStream();
            XmlWriter myWriter = XmlWriter.Create(myStream);

            myWriter.WriteStartDocument();

            myWriter.WriteStartElement("wp", "Notification", "WPNotification");
            myWriter.WriteStartElement("wp", "Toast", "WPNotification");
            myWriter.WriteStartElement("wp", "Text1", "WPNotification");
            myWriter.WriteValue(ToastTitle);
            myWriter.WriteEndElement();

            myWriter.WriteStartElement("wp", "Text2", "WPNotification");
            myWriter.WriteValue(ToastMessage);
            myWriter.WriteEndElement();

            myWriter.WriteEndElement();
            myWriter.WriteEndDocument();

            //Transfer Xml Output for myWriter's buffer to myStream.
            myWriter.Flush();

            //Send this message to all subscribed devices.
            sendPushToSpecifiedURI(myStream.ToArray(), "toast", ClientURI);
        }

        //Sends the specified Tile Title, Tile Count and Tile Image URL as a Tile Notification which can
        //only received while the subscribed phone app is NOT running. The maximum allowed size of the 
        //Tile Image is 80kb, with a maximum download time of 15 seconds. Only Tile image URLs from the 
        //domains listed in Push Client's ListOfAllowedDomains will be accepted. The Tile Title will overwrite
        //the text that labels the tile on the phone's menu, and the Tile Count is shown as a numeric value
        //displayed on top of the Tile Image, visually.
        public void PushTileUpdate(string TileTitle, int TileCount, string TileImageURL)
        {
            //Use XmlWriter to construct notification structure/contents.
            MemoryStream myStream = new MemoryStream();
            XmlWriter myWriter = XmlWriter.Create(myStream);

            myWriter.WriteStartDocument();

            myWriter.WriteStartElement("wp", "Notification", "WPNotification");
            myWriter.WriteStartElement("wp", "Tile", "WPNotification");
            myWriter.WriteStartElement("wp", "BackgroundImage", "WPNotification");
            myWriter.WriteValue(TileImageURL);
            myWriter.WriteEndElement();

            myWriter.WriteStartElement("wp", "Count", "WPNotification");
            myWriter.WriteValue(TileCount);
            myWriter.WriteEndElement();

            myWriter.WriteStartElement("wp", "Title", "WPNotification");
            myWriter.WriteValue(TileTitle);
            myWriter.WriteEndElement();

            myWriter.WriteEndElement();
            myWriter.WriteEndDocument();

            //Transfer Xml Output for myWriter's buffer to myStream.
            myWriter.Flush();

            //Send this message to all subscribed devices.
            sendPushToSubscribedURIs(myStream.ToArray(), "tile");

        }

        public void PushTileUpdateUnique(string TileTitle, int TileCount, string TileImageURL, Uri ClientURI)
        {
            //Use XmlWriter to construct notification structure/contents.
            MemoryStream myStream = new MemoryStream();
            XmlWriter myWriter = XmlWriter.Create(myStream);

            myWriter.WriteStartDocument();

            myWriter.WriteStartElement("wp", "Notification", "WPNotification");
            myWriter.WriteStartElement("wp", "Tile", "WPNotification");
            myWriter.WriteStartElement("wp", "BackgroundImage", "WPNotification");
            myWriter.WriteValue(TileImageURL);
            myWriter.WriteEndElement();

            myWriter.WriteStartElement("wp", "Count", "WPNotification");
            myWriter.WriteValue(TileCount);
            myWriter.WriteEndElement();

            myWriter.WriteStartElement("wp", "Title", "WPNotification");
            myWriter.WriteValue(TileTitle);
            myWriter.WriteEndElement();

            myWriter.WriteEndElement();
            myWriter.WriteEndDocument();

            //Transfer Xml Output for myWriter's buffer to myStream.
            myWriter.Flush();

            //Send this message to all subscribed devices.
            sendPushToSpecifiedURI(myStream.ToArray(), "tile", ClientURI);

        }

        //This enum gives symbolic names to the numeric values used to "batch together" push notifications
        //according to the specified time intervals. You can send them immediately, within 450 seconds or 
        //900 seconds. Allowing the Notification Service to batch notifications will let the service 
        //group messages together with notifications from other apps to save phone battery life.
        public enum BatchingIntervalValue
        {
            ImmediateTile = 1,
            ImmediateToast = 2,
            ImmediateRaw = 3,
            Wait450SecondsTile = 11,
            Wait450SecondsToast = 12,
            Wait450SecondsRaw = 13,
            Wait900SecondsTile = 21,
            Wait900SecondsToast = 22,
            Wait900SecondsRaw = 23
        }

        //Iterates through SubcriptionURIs, sending the constructed Push Notification to every subscribed 
        //device via the Microsoft Push Notification Service. The payload of the notification, which is
        //passed in, is packaged into an HTTP POST message that defines headers containing the notification
        //type and batching interval.
        private static void sendPushToSubscribedURIs(byte[] pushPayload, string notificationType)
        {
            //Iterate through SubscriptionURIs
            foreach (var thisURI in SubscriptionURIs.Values)
            {
                //Add headers to HTTP Post message
                var myRequest = (HttpWebRequest)WebRequest.Create(thisURI); //Push Client's channelURI
                myRequest.Method = WebRequestMethods.Http.Post;
                myRequest.ContentType = "text/xml";
                myRequest.ContentLength = pushPayload.Length;
                myRequest.Headers.Add("X-MessageID", Guid.NewGuid().ToString()); //gives this message a unique ID

                //Customize or exclude the X-WindowsPhone-Target header based on the notification
                switch (notificationType)
                {
                    case "toast":
                        myRequest.Headers["X-WindowsPhone-Target"] = "toast";
                        myRequest.Headers.Add("X-NotificationClass", ((int)BatchingIntervalValue.ImmediateToast).ToString());
                        break;
                    case "tile":
                        myRequest.Headers["X-WindowsPhone-Target"] = "token";
                        myRequest.Headers.Add("X-NotificationClass", ((int)BatchingIntervalValue.ImmediateTile).ToString());
                        break;
                    case "raw":
                        myRequest.Headers.Add("X-NotificationClass", ((int)BatchingIntervalValue.ImmediateRaw).ToString());
                        //Raw notifications do not specify a value for the X-WindowsPhone-Target header.
                        break;
                }

                //merge headers with payload.
                using (var requestStream = myRequest.GetRequestStream())
                {
                    requestStream.Write(pushPayload, 0, pushPayload.Length);
                }

                //Send notification to this phone !
                var response = (HttpWebResponse)myRequest.GetResponse();
            }
        }


        //Iterates through SubcriptionURIs, sending the constructed Push Notification to every subscribed 
        //device via the Microsoft Push Notification Service. The payload of the notification, which is
        //passed in, is packaged into an HTTP POST message that defines headers containing the notification
        //type and batching interval.
        private static void sendPushToSpecifiedURI(byte[] pushPayload, string notificationType, Uri ClientURI)
        {
            //Add headers to HTTP Post message
            var myRequest = (HttpWebRequest)WebRequest.Create(ClientURI); //Push Client's channelURI
            myRequest.Method = WebRequestMethods.Http.Post;
            myRequest.ContentType = "text/xml";
            myRequest.ContentLength = pushPayload.Length;
            myRequest.Headers.Add("X-MessageID", Guid.NewGuid().ToString()); //gives this message a unique ID

            //Customize or exclude the X-WindowsPhone-Target header based on the notification
            switch (notificationType)
            {
                case "toast":
                    myRequest.Headers["X-WindowsPhone-Target"] = "toast";
                    myRequest.Headers.Add("X-NotificationClass", ((int)BatchingIntervalValue.ImmediateToast).ToString());
                    break;
                case "tile":
                    myRequest.Headers["X-WindowsPhone-Target"] = "token";
                    myRequest.Headers.Add("X-NotificationClass", ((int)BatchingIntervalValue.ImmediateTile).ToString());
                    break;
                case "raw":
                    myRequest.Headers.Add("X-NotificationClass", ((int)BatchingIntervalValue.ImmediateRaw).ToString());
                    //Raw notifications do not specify a value for the X-WindowsPhone-Target header.
                    break;
            }

            //merge headers with payload.
            using (var requestStream = myRequest.GetRequestStream())
            {
                requestStream.Write(pushPayload, 0, pushPayload.Length);
            }

            //Send notification to this phone !
            var response = (HttpWebResponse)myRequest.GetResponse();
            
        }


        //Valider une demande
        public void ValidateDemande(int IDDemande, int IDTaxi)
        {
            bool result = false;
            string sendTitle = "Info : ";
            string sendText = "Votre demande a été prise en compte par un taxi";

            result = UpdateTaxiEtatDemandeClient(IDDemande, IDTaxi, 2);

            if(result == true)
            {
                Demande maDemande = SelectADemandeClient(IDDemande);
                Client monClient = SelectAClientByID(maDemande.ID_Client);
                Uri thisURI = new Uri(monClient.URIClient);
                PushRawDataUnique(sendText, thisURI);
                PushToastUnique(sendTitle, sendText, thisURI);
            }
        }


        //Annuler une demande par le client
        public void AnnulerDemandeClient(int IDDemande)
        {
            bool result = false;
            string sendTitle = "Info : ";
            string sendText = "Votre demande a été annulée par le client";

            result = UpdateEtatDemandeClient(IDDemande, 4);

            if (result == true)
            {
                Demande maDemande = SelectADemandeClient(IDDemande);
                Taxi monTaxi = SelectATaxiByID(maDemande.ID_Taxi);
                Uri thisURI = new Uri(monTaxi.URITaxi);
                PushRawDataUnique(sendText, thisURI);
                PushToastUnique(sendTitle, sendText, thisURI);
            }
        }

        //Annuler une demande par le taxi
        public void AnnulerDemandeTaxi(int IDDemande)
        {
            bool result = false;
            string sendTitle = "Info : ";
            string sendText = "Votre demande a été annulée par le taxi";

            result = UpdateEtatDemandeClient(IDDemande, 4);

            if (result == true)
            {
                Demande maDemande = SelectADemandeClient(IDDemande);
                Client monClient = SelectAClientByID(maDemande.ID_Client);
                Uri thisURI = new Uri(monClient.URIClient);
                PushRawDataUnique(sendText, thisURI);
                PushToastUnique(sendTitle, sendText, thisURI);
            }
        }


        //Select all CLIENT
        public List<Client> SelectAllClient()
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"SELECT * FROM CLIENT WHERE Notification_Client = 1";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    DataTable myDataTable = new DataTable();
                    SqlDataAdapter myDataAdapter;

                    List<Client> lClient = new List<Client>();

                    try
                    {
                        // Select data into database
                        myCommand.Connection.Open();
                        
                        // create data adapter
                        myDataAdapter = new SqlDataAdapter(myCommand);
                        // this will query your database and return the result to your datatable
                        myDataAdapter.Fill(myDataTable);

                        foreach (DataRow row in myDataTable.Rows)
                        {
                            int monID = Convert.ToInt32(row["ID_Client"]);
                            string monURI = row["URI_Client"].ToString();
                            string monGUID = row["ID_WP_Client"].ToString();
                            bool maNotification = Convert.ToBoolean(row["Notification_Client"]);
                            lClient.Add(new Client(monID, monGUID, monURI, maNotification));
                        }

                        myDataAdapter.Dispose();
                        myCommand.Connection.Close();

                        return lClient;
                        
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return lClient;
                    }
                }
            }
        }

        //Select a CLIENT
        public Client SelectAClient(Guid phoneID)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"SELECT * FROM CLIENT WHERE ID_WP_Client = @IDWPClient";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDWPClient", SqlDbType.VarChar, 100));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDWPClient"].Value = phoneID.ToString();

                    DataTable myDataTable = new DataTable();
                    SqlDataAdapter myDataAdapter;
                    Client cli = new Client();

                    try
                    {
                        // Select data into database
                        myCommand.Connection.Open();

                        // create data adapter
                        myDataAdapter = new SqlDataAdapter(myCommand);
                        // this will query your database and return the result to your datatable
                        myDataAdapter.Fill(myDataTable);

                        foreach (DataRow row in myDataTable.Rows)
                        {
                            int monID = Convert.ToInt32(row["ID_Client"]);
                            string monGUID = row["ID_WP_Client"].ToString();
                            string monURI = row["URI_Client"].ToString();
                            bool maNotification = Convert.ToBoolean(row["Notification_Client"]);
                            cli = new Client(monID, monGUID, monURI, maNotification);
                        }

                        myDataAdapter.Dispose();
                        myCommand.Connection.Close();

                        return cli;

                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return cli;
                    }
                }
            }
        }

        //Select a CLIENT
        public Client SelectAClientByID(int IDClient)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"SELECT * FROM CLIENT WHERE ID_Client = @IDClient";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDClient", SqlDbType.Int));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDClient"].Value = IDClient;

                    DataTable myDataTable = new DataTable();
                    SqlDataAdapter myDataAdapter;
                    Client cli = new Client();

                    try
                    {
                        // Select data into database
                        myCommand.Connection.Open();

                        // create data adapter
                        myDataAdapter = new SqlDataAdapter(myCommand);
                        // this will query your database and return the result to your datatable
                        myDataAdapter.Fill(myDataTable);

                        foreach (DataRow row in myDataTable.Rows)
                        {
                            int monID = Convert.ToInt32(row["ID_Client"]);
                            string monGUID = row["ID_WP_Client"].ToString();
                            string monURI = row["URI_Client"].ToString();
                            bool maNotification = Convert.ToBoolean(row["Notification_Client"]);
                            cli = new Client(monID, monGUID, monURI, maNotification);
                        }

                        myDataAdapter.Dispose();
                        myCommand.Connection.Close();

                        return cli;

                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return cli;
                    }
                }
            }
        }

        //Insert data into CLIENT
        public bool InsertClient(Guid IDClient, Uri URIClient)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"INSERT INTO CLIENT(ID_WP_Client, URI_Client, Notification_Client) VALUES(@IDClient, @URIClient, @Notification)";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDClient", SqlDbType.VarChar, 100));
                    myCommand.Parameters.Add(new SqlParameter("@URIClient", SqlDbType.VarChar, 200));
                    myCommand.Parameters.Add(new SqlParameter("@Notification", SqlDbType.Bit));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDClient"].Value = IDClient.ToString();
                    myCommand.Parameters["@URIClient"].Value = URIClient.ToString();
                    myCommand.Parameters["@Notification"].Value = true; 

                    try
                    {
                        // Insert data into database
                        myCommand.Connection.Open();
                        int nbInsert = myCommand.ExecuteNonQuery();
                        myCommand.Connection.Close();

                        if (nbInsert > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return false;
                    }
                }
            }
        }

        //Update data into CLIENT
        public bool UpdateClient(Guid IDClient, Uri URIClient, Boolean Notification)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"UPDATE CLIENT SET URI_Client=@URIClient, Notification_Client=@Notification WHERE ID_WP_Client=@IDClient";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDClient", SqlDbType.VarChar, 100));
                    myCommand.Parameters.Add(new SqlParameter("@URIClient", SqlDbType.VarChar, 200));
                    myCommand.Parameters.Add(new SqlParameter("@Notification", SqlDbType.Bit));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDClient"].Value = IDClient.ToString();
                    myCommand.Parameters["@URIClient"].Value = URIClient.ToString();
                    myCommand.Parameters["@Notification"].Value = Notification;

                    try
                    {
                        // Insert data into database
                        myCommand.Connection.Open();
                        int nbInsert = myCommand.ExecuteNonQuery();
                        myCommand.Connection.Close();

                        if (nbInsert > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return false;
                    }
                }
            }
        }


        //Update data into CLIENT
        public bool UpdateClientURI(Guid IDClient, Uri URIClient)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"UPDATE CLIENT SET URI_Client=@URIClient WHERE ID_WP_Client=@IDClient";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDClient", SqlDbType.VarChar, 100));
                    myCommand.Parameters.Add(new SqlParameter("@URIClient", SqlDbType.VarChar, 200));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDClient"].Value = IDClient.ToString();
                    myCommand.Parameters["@URIClient"].Value = URIClient.ToString();

                    try
                    {
                        // Insert data into database
                        myCommand.Connection.Open();
                        int nbInsert = myCommand.ExecuteNonQuery();
                        myCommand.Connection.Close();

                        if (nbInsert > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return false;
                    }
                }
            }
        }


        //Select all DEMANDE_CLIENT
        public List<Demande> SelectAllDemandeClient()
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"SELECT * FROM DEMANDE_CLIENT WHERE ID_Etat = 1";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    DataTable myDataTable = new DataTable();
                    SqlDataAdapter myDataAdapter;

                    List<Demande> lDemande = new List<Demande>();
                    DateTime myDateTime = DateTime.Now;

                    try
                    {
                        // Select data into database
                        myCommand.Connection.Open();

                        // create data adapter
                        myDataAdapter = new SqlDataAdapter(myCommand);
                        // this will query your database and return the result to your datatable
                        myDataAdapter.Fill(myDataTable);

                        foreach (DataRow row in myDataTable.Rows)
                        {
                            int monIDDemande = Convert.ToInt32(row["ID_Demande"]);
                            int monIDClient = Convert.ToInt32(row["ID_Client"]);
                            int monIDEtat = Convert.ToInt32(row["ID_Etat"]);
                            decimal maLongitudeOrigine = Convert.ToDecimal(row["Longitude_Origine"]);
                            decimal maLatitudeOrigine = Convert.ToDecimal(row["Latitude_Origine"]);
                            string monAdresseOrigine = row["Adresse_Origine"].ToString();
                            decimal maLongitudeDestination = Convert.ToDecimal(row["Longitude_Destination"]);
                            decimal maLatitudeDestination = Convert.ToDecimal(row["Latitude_Destination"]);
                            string monAdresseDestination = row["Adresse_Destination"].ToString();
                            int monNombrePersonne = Convert.ToInt32(row["Nombre_Personne"]);
                            DateTime maDateDebut = Convert.ToDateTime(row["Date_Debut"]);
                            lDemande.Add(new Demande(monIDDemande, monIDClient, monIDEtat, maLongitudeOrigine, maLatitudeOrigine, monAdresseOrigine, maLongitudeDestination, maLatitudeDestination, monAdresseDestination, monNombrePersonne, maDateDebut));
                        }

                        myDataAdapter.Dispose();
                        myCommand.Connection.Close();

                        return lDemande;

                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return lDemande;
                    }
                }
            }
        }
       

        //Select a DEMANDE_CLIENT
        public Demande SelectADemandeClient(int IDDemande)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"SELECT * FROM DEMANDE_CLIENT WHERE ID_Demande = @IDDemande";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    DataTable myDataTable = new DataTable();
                    SqlDataAdapter myDataAdapter;

                    Demande maDemande = new Demande();
                    DateTime myDateTime = DateTime.Now;

                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDDemande", SqlDbType.Int));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDDemande"].Value = IDDemande;

                    try
                    {
                        // Select data into database
                        myCommand.Connection.Open();

                        // create data adapter
                        myDataAdapter = new SqlDataAdapter(myCommand);
                        // this will query your database and return the result to your datatable
                        myDataAdapter.Fill(myDataTable);

                        foreach (DataRow row in myDataTable.Rows)
                        {
                            int monIDDemande = Convert.ToInt32(row["ID_Demande"]);
                            int monIDClient = Convert.ToInt32(row["ID_Client"]);
                            int monIDEtat = Convert.ToInt32(row["ID_Etat"]);
                            decimal maLongitudeOrigine = Convert.ToDecimal(row["Longitude_Origine"]);
                            decimal maLatitudeOrigine = Convert.ToDecimal(row["Latitude_Origine"]);
                            string monAdresseOrigine = row["Adresse_Origine"].ToString();
                            decimal maLongitudeDestination = Convert.ToDecimal(row["Longitude_Destination"]);
                            decimal maLatitudeDestination = Convert.ToDecimal(row["Latitude_Destination"]);
                            string monAdresseDestination = row["Adresse_Destination"].ToString();
                            int monNombrePersonne = Convert.ToInt32(row["Nombre_Personne"]);
                            DateTime maDateDebut = Convert.ToDateTime(row["Date_Debut"]);
                            maDemande = new Demande(monIDDemande, monIDClient, monIDEtat, maLongitudeOrigine, maLatitudeOrigine, monAdresseOrigine, maLongitudeDestination, maLatitudeDestination, monAdresseDestination, monNombrePersonne, maDateDebut);
                        }

                        myDataAdapter.Dispose();
                        myCommand.Connection.Close();

                        return maDemande;

                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return maDemande;
                    }
                }
            }
        }

        //Insert data into DEMANDE_CLIENT
        public bool InsertDemandeClient(int IDClient, Double LongitudeOrigine, Double LatitudeOrigine, string AdresseOrigine, Double LongitudeDestination, Double LatitudeDestination, string AdresseDestination, int NombrePersonne)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"INSERT INTO DEMANDE_CLIENT(ID_Client, ID_Etat, Longitude_Origine, Latitude_Origine, Adresse_Origine, Longitude_Destination, Latitude_Destination, Adresse_Destination, Nombre_Personne, Date_Debut) VALUES(@IDClient, @IDEtat, @LongitudeOrigine, @LatitudeOrigine, @AdresseOrigine, @LongitudeDestination, @LatitudeDestination, @AdresseDestination, @NombrePersonne, @DateDebut)";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    DateTime myDateTime = DateTime.Now;

                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDClient", SqlDbType.Int));
                    myCommand.Parameters.Add(new SqlParameter("@IDEtat", SqlDbType.SmallInt));
                    myCommand.Parameters.Add(new SqlParameter("@LongitudeOrigine", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@LatitudeOrigine", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@AdresseOrigine", SqlDbType.Text));
                    myCommand.Parameters.Add(new SqlParameter("@LongitudeDestination", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@LatitudeDestination", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@AdresseDestination", SqlDbType.Text));
                    myCommand.Parameters.Add(new SqlParameter("@NombrePersonne", SqlDbType.SmallInt));
                    myCommand.Parameters.Add(new SqlParameter("@DateDebut", SqlDbType.DateTime));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDClient"].Value = IDClient;
                    myCommand.Parameters["@IDEtat"].Value = 1;
                    myCommand.Parameters["@LongitudeOrigine"].Value = LongitudeOrigine;
                    myCommand.Parameters["@LatitudeOrigine"].Value = LatitudeOrigine;
                    myCommand.Parameters["@AdresseOrigine"].Value = AdresseOrigine;
                    myCommand.Parameters["@LongitudeDestination"].Value = LongitudeDestination;
                    myCommand.Parameters["@LatitudeDestination"].Value = LatitudeDestination;
                    myCommand.Parameters["@AdresseDestination"].Value = AdresseDestination;
                    myCommand.Parameters["@NombrePersonne"].Value = NombrePersonne;
                    myCommand.Parameters["@DateDebut"].Value = myDateTime;

                    try
                    {
                        // Insert data into database
                        myCommand.Connection.Open();
                        int nbInsert = myCommand.ExecuteNonQuery();
                        myCommand.Connection.Close();

                        if (nbInsert > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return false;
                    }
                }
            }
        }

        //Update data into DEMANDE_CLIENT
        public bool UpdateDemandeClient(int IDDemande, int IDTaxi, int IDEtat, Double LongitudeOrigine, Double LatitudeOrigine, string AdresseOrigine, Double LongitudeDestination, Double LatitudeDestination, string AdresseDestination, int NombrePersonne, DateTime DateFin)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"UPDATE DEMANDE_CLIENT SET ID_Taxi=@IDTaxi, ID_Etat=@IDEtat, Longitude_Origine=@LongitudeOrigine, Latitude_Origine=@LatitudeOrigine, Adresse_Origine=@AdresseOrigine, Longitude_Destination=@LongitudeDestination, Latitude_Destination=@LatitudeDestination, Adresse_Destination=@AdresseDestination, Nombre_Personne=@NombrePersonne, Date_Fin=@DateFin WHERE ID_Demande=@IDDemande";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDDemande", SqlDbType.Int));
                    myCommand.Parameters.Add(new SqlParameter("@IDTaxi", SqlDbType.Int));
                    myCommand.Parameters.Add(new SqlParameter("@IDEtat", SqlDbType.SmallInt));
                    myCommand.Parameters.Add(new SqlParameter("@LongitudeOrigine", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@LatitudeOrigine", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@AdresseOrigine", SqlDbType.Text));
                    myCommand.Parameters.Add(new SqlParameter("@LongitudeDestination", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@LatitudeDestination", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@AdresseDestination", SqlDbType.Text));
                    myCommand.Parameters.Add(new SqlParameter("@NombrePersonne", SqlDbType.SmallInt));
                    myCommand.Parameters.Add(new SqlParameter("@DateFin", SqlDbType.DateTime));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDDemande"].Value = IDDemande;
                    myCommand.Parameters["@IDTaxi"].Value = IDTaxi;
                    myCommand.Parameters["@IDEtat"].Value = IDEtat;
                    myCommand.Parameters["@LongitudeOrigine"].Value = LongitudeOrigine;
                    myCommand.Parameters["@LatitudeOrigine"].Value = LatitudeOrigine;
                    myCommand.Parameters["@AdresseOrigine"].Value = AdresseOrigine;
                    myCommand.Parameters["@LongitudeDestination"].Value = LongitudeDestination;
                    myCommand.Parameters["@LatitudeDestination"].Value = LatitudeDestination;
                    myCommand.Parameters["@AdresseDestination"].Value = AdresseDestination;
                    myCommand.Parameters["@NombrePersonne"].Value = NombrePersonne;
                    myCommand.Parameters["@DateFin"].Value = DateFin;
                    
                    try
                    {
                        // Insert data into database
                        myCommand.Connection.Open();
                        int nbInsert = myCommand.ExecuteNonQuery();
                        myCommand.Connection.Close();

                        if (nbInsert > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return false;
                    }
                }
            }
        }

        //Update data into DEMANDE_CLIENT
        public bool UpdateTaxiEtatDemandeClient(int IDDemande, int IDTaxi, int IDEtat)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"UPDATE DEMANDE_CLIENT SET ID_Taxi=@IDTaxi, ID_Etat=@IDEtat WHERE ID_Demande=@IDDemande";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDDemande", SqlDbType.Int));
                    myCommand.Parameters.Add(new SqlParameter("@IDTaxi", SqlDbType.Int));
                    myCommand.Parameters.Add(new SqlParameter("@IDEtat", SqlDbType.SmallInt));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDDemande"].Value = IDDemande;
                    myCommand.Parameters["@IDTaxi"].Value = IDTaxi;
                    myCommand.Parameters["@IDEtat"].Value = IDEtat;

                    if (IDEtat == 1)
                    {
                        myCommand.Parameters["@IDTaxi"].Value = DBNull.Value;
                    }

                    try
                    {
                        // Insert data into database
                        myCommand.Connection.Open();
                        int nbInsert = myCommand.ExecuteNonQuery();
                        myCommand.Connection.Close();

                        if (nbInsert > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return false;
                    }
                }
            }
        }

        //Update data into DEMANDE_CLIENT
        public bool UpdateEtatDemandeClient(int IDDemande, int IDEtat)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                DateTime myDateTime = DateTime.Now;
                string cmdText = @"UPDATE DEMANDE_CLIENT SET ID_Etat=@IDEtat, Date_Fin=@DateFin WHERE ID_Demande=@IDDemande";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDDemande", SqlDbType.Int));
                    myCommand.Parameters.Add(new SqlParameter("@IDEtat", SqlDbType.SmallInt));
                    myCommand.Parameters.Add(new SqlParameter("@DateFin", SqlDbType.DateTime));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDDemande"].Value = IDDemande;
                    myCommand.Parameters["@IDEtat"].Value = IDEtat;
                    myCommand.Parameters["@DateFin"].Value = DBNull.Value;

                    if(IDEtat == 3 || IDEtat == 4){
                        myCommand.Parameters["@DateFin"].Value = myDateTime;
                    }

                    try
                    {
                        // Insert data into database
                        myCommand.Connection.Open();
                        int nbInsert = myCommand.ExecuteNonQuery();
                        myCommand.Connection.Close();

                        if (nbInsert > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return false;
                    }
                }
            }
        }

        //Select a Taxi by ID
        public Taxi SelectATaxiByID(int IDTaxi)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"SELECT * FROM TAXI WHERE ID_Taxi = @IDTaxi";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDTaxi", SqlDbType.Int));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDTaxi"].Value = IDTaxi;

                    DataTable myDataTable = new DataTable();
                    SqlDataAdapter myDataAdapter;
                    Taxi monTaxi = new Taxi();

                    try
                    {
                        // Select data into database
                        myCommand.Connection.Open();

                        // create data adapter
                        myDataAdapter = new SqlDataAdapter(myCommand);
                        // this will query your database and return the result to your datatable
                        myDataAdapter.Fill(myDataTable);

                        foreach (DataRow row in myDataTable.Rows)
                        {
                            int monID = Convert.ToInt32(row["ID_Taxi"]);
                            string monGUID = row["ID_WP_Taxi"].ToString();
                            string monURI = row["URI_Taxi"].ToString();
                            string monMDP = row["MdP"].ToString();
                            decimal maLongitudeTaxi = Convert.ToDecimal(row["Longitude_Taxi"]);
                            decimal maLatitudeTaxi = Convert.ToDecimal(row["Latitude_Taxi"]);
                            bool maNotification = Convert.ToBoolean(row["Notification_Taxi"]);
                            DateTime maDateInscription = Convert.ToDateTime(row["Date_Inscription"]);
                            monTaxi = new Taxi(monID, monGUID, monURI, monMDP, maLongitudeTaxi, maLatitudeTaxi, maDateInscription, maNotification);
                        }

                        myDataAdapter.Dispose();
                        myCommand.Connection.Close();

                        return monTaxi;

                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return monTaxi;
                    }
                }
            }
        }

        //Insert data into TAXI
        public Taxi InsertTaxi(Guid IDTaxi, Uri URITaxi, string MdP, decimal LongitudeTaxi, decimal LatitudeTaxi)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(MdP);
            encodedBytes = md5.ComputeHash(originalBytes);

            Taxi monTaxi = new Taxi();

            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"INSERT INTO TAXI(ID_WP_Taxi, URI_Taxi, MdP, Longitude_Taxi, Latitude_Taxi, Date_Inscription, Notification_Taxi) VALUES(@IDTaxi, @URITaxi, @MdP, @LongitudeTaxi, @LatitudeTaxi, @DateInscription, @Notification)";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    DateTime myDateTime = DateTime.Now;

                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDTaxi", SqlDbType.VarChar, 100));
                    myCommand.Parameters.Add(new SqlParameter("@URITaxi", SqlDbType.VarChar, 200));
                    myCommand.Parameters.Add(new SqlParameter("@LongitudeTaxi", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@LatitudeTaxi", SqlDbType.Float));
                    myCommand.Parameters.Add(new SqlParameter("@MdP", SqlDbType.VarChar, 50));
                    myCommand.Parameters.Add(new SqlParameter("@DateInscription", SqlDbType.Date));
                    myCommand.Parameters.Add(new SqlParameter("@Notification", SqlDbType.Bit));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDTaxi"].Value = IDTaxi.ToString();
                    myCommand.Parameters["@URITaxi"].Value = URITaxi.ToString();
                    myCommand.Parameters["@MdP"].Value = BitConverter.ToString(encodedBytes);
                    myCommand.Parameters["@LongitudeTaxi"].Value = LongitudeTaxi;
                    myCommand.Parameters["@LatitudeTaxi"].Value = LatitudeTaxi;
                    myCommand.Parameters["@DateInscription"].Value = myDateTime;
                    myCommand.Parameters["@Notification"].Value = true;

                    try
                    {
                        // Insert data into database
                        myCommand.Connection.Open();
                        int nbInsert = myCommand.ExecuteNonQuery();
                        myCommand.Connection.Close();

                        if (nbInsert > 0)
                        {
                            monTaxi = ConnectTaxi(IDTaxi, MdP, LongitudeTaxi, LatitudeTaxi);
                            return monTaxi;
                        }
                        else
                        {
                            return monTaxi;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return monTaxi;
                    }
                }
            }
        }


        //Update data into TAXI
        public bool UpdateTaxiLocalisation(Guid IDTaxi, decimal LongitudeTaxi, decimal LatitudeTaxi)
        {
            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                DateTime myDateTime = DateTime.Now;
                string cmdText = @"UPDATE TAXI SET Longitude_Taxi=@LongitudeTaxi, Latitude_Taxi=@LatitudeTaxi WHERE ID_WP_Taxi=@IDTaxi";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@LongitudeTaxi", SqlDbType.Decimal));
                    myCommand.Parameters.Add(new SqlParameter("@Latitude_Taxi", SqlDbType.Decimal));
                    myCommand.Parameters.Add(new SqlParameter("@IDTaxi", SqlDbType.VarChar, 100));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@LongitudeTaxi"].Value = LongitudeTaxi;
                    myCommand.Parameters["@Latitude_Taxi"].Value = LatitudeTaxi;
                    myCommand.Parameters["@IDTaxi"].Value = IDTaxi.ToString();
                    
                    try
                    {
                        // Insert data into database
                        myCommand.Connection.Open();
                        int nbInsert = myCommand.ExecuteNonQuery();
                        myCommand.Connection.Close();

                        if (nbInsert > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return false;
                    }
                }
            }
        }

        public Taxi ConnectTaxi(Guid IDTaxi, string MdP, decimal LongitudeTaxi, decimal LatitudeTaxi)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(MdP);
            encodedBytes = md5.ComputeHash(originalBytes);

            UpdateTaxiLocalisation(IDTaxi, LongitudeTaxi, LatitudeTaxi);

            // Connect to the TaxiTouch database and execute the request
            using (SqlConnection myConn = new SqlConnection(connStringBuilder.ToString()))
            {
                string cmdText = @"SELECT * FROM TAXI WHERE ID_WP_Taxi = @IDTaxi AND MDP = @MDP";

                //Création de la commande SQL 
                using (SqlCommand myCommand = new SqlCommand(cmdText, myConn))
                {
                    DataTable myDataTable = new DataTable();
                    SqlDataAdapter myDataAdapter;

                    Taxi monTaxi= new Taxi();

                    //Création et décalartion des paramètres 
                    myCommand.Parameters.Add(new SqlParameter("@IDTaxi", SqlDbType.VarChar, 100));
                    myCommand.Parameters.Add(new SqlParameter("@MDP", SqlDbType.VarChar, 50));

                    //Attribution des valeurs aux paramètres 
                    myCommand.Parameters["@IDTaxi"].Value = IDTaxi.ToString();
                    myCommand.Parameters["@MDP"].Value = BitConverter.ToString(encodedBytes);

                    try
                    {
                        // Select data into database
                        myCommand.Connection.Open();

                        // create data adapter
                        myDataAdapter = new SqlDataAdapter(myCommand);
                        // this will query your database and return the result to your datatable
                        myDataAdapter.Fill(myDataTable);

                        foreach (DataRow row in myDataTable.Rows)
                        {
                            int monIDTaxi = Convert.ToInt32(row["ID_Taxi"]);
                            string monIDWPTaxi = row["ID_WP_Taxi"].ToString();
                            string monURITaxi = row["URI_Taxi"].ToString();
                            string monMdP = row["MDP"].ToString();
                            decimal maLongitudeTaxi = Convert.ToDecimal(row["Longitude_Taxi"]);
                            decimal maLatitudeTaxi = Convert.ToDecimal(row["Latitude_Taxi"]);
                            DateTime maDateInscription = Convert.ToDateTime(row["Date_Inscription"]);
                            bool maNotificationTaxi = Convert.ToBoolean(row["Notification_Taxi"]);

                            monTaxi = new Taxi(monIDTaxi, monIDWPTaxi, monURITaxi, monMdP, maLongitudeTaxi, maLatitudeTaxi, maDateInscription, maNotificationTaxi);
                        }

                        myDataAdapter.Dispose();
                        myCommand.Connection.Close();

                        return monTaxi;

                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                        return monTaxi;
                    }
                }
            }
        }
    }
}
